package com.zzptc.zzj.a1123;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.zzj.a1123.entity.CarList;
import com.zzptc.zzj.a1123.entity.Guzhang;
import com.zzptc.zzj.a1123.service.CarLIstService;
import com.zzptc.zzj.a1123.service.GuzhangService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";//logt
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // getGuzhangInfo();
        getCarInfo();
    }

    private void getCarInfo() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://apis.juhe.cn/sxpd/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //构建接口对象 接口不能new 由retrofit来create
        CarLIstService carLIstService=retrofit.create(CarLIstService.class);
        //调用接口的方法
        Call<CarList> all = carLIstService.getAll();
        //调用Call类型中的异步访问网络的方法
        all.enqueue(new Callback<CarList>() {
            @Override
            public void onResponse(Call<CarList> call, Response<CarList> response) {
                CarList carList=response.body();
                Log.d(TAG,"onResponse"+carList.getResult().getData());
            }

            @Override
            public void onFailure(Call<CarList> call, Throwable t) {
                Log.d(TAG,"onFailure"+t.getMessage());
            }
        });
    }



    private void getGuzhangInfo() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("https://apis.juhe.cn/obdcode/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //构建接口对象 接口不能new 由retrofit来create
        GuzhangService guzhangService = retrofit.create(GuzhangService.class);
        //调用接口的到方法
        Call<Guzhang> guzhangInfo = guzhangService.getGuzhangInfo();
        //调用Call类型中的异步访问网络的方法
        guzhangInfo.enqueue(new Callback<Guzhang>() {
            @Override
            public void onResponse(Call<Guzhang> call, Response<Guzhang> response) {
                Guzhang gu = response.body();//得到所有的故障信息
                Log.d(TAG,"onResponse:"+gu.getResult().getYwhy());
            }

            @Override
            public void onFailure(Call<Guzhang> call, Throwable t) {
                Log.d(TAG,"onFailure:");
            }
        });

    }
}
